﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Actuals.CoreDomain.Business;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;
using Actuals.Data;

namespace Actuals.Business
{
    public class BusinessActual : IBusinessActual
    {

        private IDalActual _dalActuals;

        public BusinessActual(IDalActual dalActuals)
        {
            _dalActuals = dalActuals;
        }

        public BusinessActual()
        {
            _dalActuals = new DalActual();
        }

        #region IBusinessDashBoard Members

        public IList<IActual> GetActuals(DateTime fromDate, DateTime toDate)
        {
            IList<IActual> actuals = _dalActuals.GetActuals(fromDate, toDate, 0,0);
            return actuals;

        }

        public IList<IActual> GetActuals(DateTime fromDate, DateTime toDate, int projectId, int releaseId)
        {
            IList<IActual> actuals = _dalActuals.GetActuals(fromDate, toDate, projectId, releaseId);
            return actuals;

        }

       

        public IList<IActual> GetActualsByTaskId(DateTime fromDate, DateTime toDate, int taskId)
        {
            IList<IActual> actuals = _dalActuals.GetActualsByTaskId(fromDate, toDate, taskId);
            return actuals;

        }

        public IList<IActual> GetActualsForEmployee(DateTime fromDate, DateTime toDate,  int employeeId)
        {
            IList<IActual> actuals = _dalActuals.GetActualsForEmployee(fromDate, toDate, 0, 0, employeeId);
            return actuals;
        }

        public IList<IActual> GetActualsForEmployee(DateTime fromDate, DateTime toDate, int projectId, int releaseId, int employeeId)
        {
            IList<IActual> actuals = _dalActuals.GetActualsForEmployee(fromDate, toDate, projectId, releaseId, employeeId);
            return actuals;
        }

        public IList<IActual> GetActualsOnlyForDevelopers(DateTime fromDate, DateTime toDate)
        {
            IList<IActual> actuals = _dalActuals.GetActualsOnlyForDevelopers(fromDate, toDate, 0, 0);
            return actuals;

        }


        public IList<IActual> GetActualsOnlyForDevelopers(DateTime fromDate, DateTime toDate, int projectId, int releaseId)
        {
            IList<IActual> actuals = _dalActuals.GetActualsOnlyForDevelopers(fromDate, toDate, projectId, releaseId);
            return actuals;

        }

        public IList<IActualBreakDownByEmployee> GetActualsGroupByDevelopers(DateTime fromDate, DateTime toDate)
        {
            return GetActualsGroupByDevelopers(fromDate, toDate, 0, 0);

        }

        public IList<IActualBreakDownByEmployee> GetActualsGroupByDevelopers(DateTime fromDate, DateTime toDate, int projectId, int releaseId)
        {
            IList<IActual> actuals = _dalActuals.GetActualsOnlyForDevelopers(fromDate, toDate, projectId, releaseId);

            var sums = actuals.ToList().GroupBy(x => new { x.EmployeeId }).Select(group => new
            {
                group.Key.EmployeeId,
                TotalHours = group.Sum(x => x.NoOfHours)
            });

            IList<IActualBreakDownByEmployee> actualBreakDownsByEmployee = new List<IActualBreakDownByEmployee>();
            foreach (var grouping in sums)
            {

                int employeeId = grouping.EmployeeId;
                IBusinessEmployee businessEmployee = new BusinessEmployee();

                IEmployee employee = businessEmployee.GetEmployee(employeeId);
                IActualBreakDownByEmployee actualBreakDownByEmployee = new ActualBreakDownByEmployee() ;
                actualBreakDownByEmployee.NickName = employee.NickName;
                actualBreakDownByEmployee.EmployeeId = employeeId ;
                actualBreakDownByEmployee.TotalHours = grouping.TotalHours;

                actualBreakDownsByEmployee.Add(actualBreakDownByEmployee);

            }

            return actualBreakDownsByEmployee;

        }

        # endregion

        #region IBusinessActual Members


        public IList<IActual> GetActualsForAWeek(int EmployeeId, int ProjectId, int ReleaseId)
        {
            throw new NotImplementedException();
        }

        public bool DeleteActual(int actualId)
        {
            return _dalActuals.DeleteActual(actualId);
        }

        public bool UpdateActual(IActual actual)
        {
            return _dalActuals.UpdateActual(actual);
        }


        public bool AddActual(IActual actual)
        {
            return _dalActuals.AddActual(actual);
        }

        public IActual GetActual(Int64 actualId)
        {
            return _dalActuals.GetActual(actualId);
        }
        public bool BtnAddActualDisable(int projectId, int releaseId)
        {
              DateTime releaseEndDate;
            DateTime releaseStartDate;

            IBusinessRelease businessRelease = new BusinessRelease();
            IRelease release = businessRelease.GetRelease(projectId, releaseId);
            releaseEndDate = release.EndDate;
            releaseStartDate = release.StartDate;
            if (releaseEndDate >= DateTime.Now)
                return true;
            else
                return false;


        }


        #endregion
    }
}
